package algorithm.poj.p1000;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLDecoder;

/**
 * 分析：
 * 对比2591，2545
 * 实现：
 * 经验：
 * 教训：
 * 
 * @author wong.tong@gmail.com
 *
 */
public class P1338 {

	public static void main(String[] args) throws Exception {

		InputStream input = null;
		if (false) {
			input = System.in;
		} else {
			URL url = P1338.class.getResource("P1338.txt");
			File file = new File(URLDecoder.decode(url.getPath(), "UTF-8"));
			input = new FileInputStream(file);
		}
		
		BufferedReader stdin = new BufferedReader(new InputStreamReader(input));

		Node head = new Node(1);
		Node p = head;	//记录当前数据位置
		Node q = head;	//遍历链表，记录插入位置
		
		long[] a = new long[1500];
		for (int i = 0; i < a.length; i ++) {
			a[i] = p.value;
			long x = p.value;
			q = insert(2*x, q);
			Node t = insert(3*x, q);
			t = insert(5*x, t);
			p = p.next;
		}
		
		StringBuffer sb = new StringBuffer("");
		String line = stdin.readLine();
        while (line != null && !"0".equals(line.trim())) {
        	int n = Integer.valueOf(line);
        	sb.append(a[n-1]).append("\n");
	        line = stdin.readLine();
        }
        System.out.println(sb);
	}
	
	private static class Node {
		long value;
		Node next;
		
		public Node(long value) {
			this.value = value;
			this.next = null;
		}
	}
	
	/**
	 * 从s指定的位置，往后找到插入位置，并且返回这个插入位置
	 * @param value
	 * @param s
	 */
	private static Node insert(long value, Node s) {
		
		if (s == null || s.value > value) {
			throw new RuntimeException("Never occurs!");
		} else {
			Node p = s;
			Node q = s.next;
			while (true) {
				if (q == null || q.value > value) {
					break;
				} else {
					p = q;
					q = q.next;
				}
			}
			if (p.value < value) {
				Node n = new Node(value);
				p.next = n;
				n.next = q;
				return n;
			} else {
				return p;
			}
		}
	}
}
